//
//  NoteAlert.swift
//  CheckmeRing
//
//  Created by 董鹏 on 2022/2/20.
//

import UIKit
import Toast_Swift

class NoteAlert: UIView {
    
    typealias backText = (_ text: String) ->()
    var backText:backText?

    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var confirmBtn: UIButton!
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var placeholderLabel: UILabel!
    
    private let maxCount = 200
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
            self.setUI()
        }
    }
    func setUI(){
        
        self.bgView.maskCorners(corners: UIRectCorner.init(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.topRight.rawValue), cornerRadi: 20)
        self.cancelBtn.layer.borderColor = hexColor("3468E2")!.cgColor
        self.cancelBtn.setTitleColor(hexColor("3468E2"), for: .normal)
        
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 0.36, green: 0.53, blue: 0.96, alpha: 1).cgColor, UIColor(red: 0.2, green: 0.4, blue: 0.88, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = self.confirmBtn.bounds
        bgLayer1.startPoint = CGPoint(x: 1.03, y: 0.67)
        bgLayer1.endPoint = CGPoint(x: 0.67, y: 0.67)
        self.confirmBtn.layer.insertSublayer(bgLayer1, at: 0)
        self.confirmBtn.layer.masksToBounds = true
        
        self.textView.layer.cornerRadius = 6
        self.textView.layer.borderWidth = 1
        self.textView.layer.borderColor = hexColor("E8E8E8")!.cgColor
        self.textView.delegate = self
        
        /// 回收键盘
        self.isUserInteractionEnabled = true
        let ges = UITapGestureRecognizer.init(target: self, action: #selector(returnKeyBoard))
        self.addGestureRecognizer(ges)
        
    }
    
    @objc func returnKeyBoard() {
        self.endEditing(true)
    }

    @IBAction func cancelBtnDidClilcked(_ sender: Any) {
        self.removeFromSuperview()
    }
    
    @IBAction func confirmBtnDidClicked(_ sender: Any) {
        if self.textView.text.count == 0 {
            getCurrentViewController()?.view.makeToast("内容を入力してください")
            return
        }
        if let backText = self.backText {
            backText(self.textView.text)
            self.removeFromSuperview()
        }
    }
    
}

extension NoteAlert: UITextViewDelegate {
    
    func textViewDidChange(_ textView: UITextView) {
        if textView.text.count > self.maxCount {
            if textView.text != nil {
                // 获取非选中状态文字范围
                let selectedRange = self.textView.markedTextRange
                // 没有非选中文字，截取多出的文字
                if selectedRange == nil {
                    let text = self.textView.text ?? ""
                    if text.count > self.maxCount {
                        let index = text.index(text.startIndex, offsetBy: self.maxCount)
                        self.textView.text = String(text[..<index])
                    }
                }
            }
        }
        countLabel.text = String(format: "%ld/\(self.maxCount)", textView.text.count)
        self.placeholderLabel.isHidden = self.textView.text.count > 0
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.count > self.maxCount {
            if textView.text != nil {
                // 获取非选中状态文字范围
                let selectedRange = self.textView.markedTextRange
                // 没有非选中文字，截取多出的文字
                if selectedRange == nil {
                    let text = self.textView.text ?? ""
                    if text.count > self.maxCount {
                        let index = text.index(text.startIndex, offsetBy: self.maxCount)
                        self.textView.text = String(text[..<index])
                    }
                }
            }
        }
        countLabel.text = String(format: "%ld/\(self.maxCount)", textView.text.count)
        self.placeholderLabel.isHidden = self.textView.text.count > 0
    }
    
}
